package edu.xpu.project.record.service;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import edu.xpu.project.record.entity.BusBespeakRecord;
import edu.xpu.project.record.repository.BusBespeakRecordRepository;

/**
 * @ClassName: BusBespeakRecordServiceImpl.java
 * @Description: BusBespeakRecordService的业务实现类
 *
 * @author liukang
 * @date 2017年5月9日 下午1:53:25
 * @version 
 * @since JDK 1.6
 */
@Service
@Transactional(readOnly=true)
public class BusBespeakRecordServiceImpl implements BusBespeakRecordService {
	
	@Autowired
	private BusBespeakRecordRepository busBespeakRecordRepository;

	@Override
	public BusBespeakRecord get(Long id) {
		return busBespeakRecordRepository.findOne(id);
	}

	@Transactional(readOnly=false)
	@Override
	public BusBespeakRecord saveOrUpdate(BusBespeakRecord t) {
		return busBespeakRecordRepository.saveAndFlush(t);
	}

	@Transactional(readOnly=false)
	@Override
	public void delete(Long id) {
		busBespeakRecordRepository.delete(id);
	}

	@Override
	public Page<BusBespeakRecord> getPageByCriterias(String pageNumberStr, String pageSizeStr,
			final BusBespeakRecord bespeakRecord) {
		Integer pageNumber = 1;
		Integer pageSize = 10;
		try {
			pageNumber = Integer.parseInt(pageNumberStr);
		} catch (Exception e) {}
		try {
			pageSize = Integer.parseInt(pageSizeStr);
		} catch (Exception e) {}
		Pageable pageable = new PageRequest(pageNumber - 1, pageSize, new Sort(Direction.DESC, "applyTime"));
		return busBespeakRecordRepository.findAll(new Specification<BusBespeakRecord>() {
			
			@Override
			public Predicate toPredicate(Root<BusBespeakRecord> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
				if (bespeakRecord == null) {
					return null;
				}
				//添加查询条件
				List<Predicate> predicates = new ArrayList<Predicate>(3);
				if (bespeakRecord.getProjectUser() != null && bespeakRecord.getProjectUser().getId() != null) {
					predicates.add(builder.equal(root.get("projectUser").get("id").as(Long.class), bespeakRecord.getProjectUser().getId()));
				}
				//添加姓名、始发站、工号
				if (bespeakRecord.getProjectUser() != null) {
					if (StringUtils.isNotBlank(bespeakRecord.getProjectUser().getName())) {
						predicates.add(builder.like(root.get("projectUser").get("name").as(String.class), "%"+bespeakRecord.getProjectUser().getName()+"%"));
					}
					if (StringUtils.isNotBlank(bespeakRecord.getProjectUser().getUserNo())) {
						predicates.add(builder.like(root.get("projectUser").get("userNo").as(String.class), "%"+bespeakRecord.getProjectUser().getUserNo()+"%"));
					}
				}
				if (bespeakRecord.getBusSchedule() != null) {
					if (bespeakRecord.getBusSchedule().getStartStation() != null && bespeakRecord.getBusSchedule().getStartStation() != -1) {
						predicates.add(builder.equal(root.get("busSchedule").get("startStation").as(Integer.class), bespeakRecord.getBusSchedule().getStartStation()));
					}
				}
				
				if (predicates.size() == 0) {
					return null;
				}
				Predicate[] predArr= new Predicate[predicates.size()];
				return builder.and(predicates.toArray(predArr));
			}
		}, pageable);
	}

	@Override
	public List<BusBespeakRecord> getBusBespeakRecordByDate(String dateStr) {
		return busBespeakRecordRepository.findByApplyTime(dateStr);
	}

	@Override
	public List<Object> getBusSchedulePersonNum(String dateStr) {
		List<Object> busSchedulePersonNum = busBespeakRecordRepository.getBusSchedulePersonNum(dateStr);
		return busSchedulePersonNum;
	}

}

